package willevaluate.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.KeyBoard;
import utils.MyLogUtils;

/**
 * Created by Administrator on 2017/4/17.
 * 优惠券
 */
@ContentView(R.layout.coupon)
public class Coupon extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.coupon_count_edt)
    private EditText coupon_count_edt;
    @ViewInject(R.id.coupon_pwd_edt)
    private EditText coupon_pwd_edt;
    @ViewInject(R.id.coupon_commit)
    private Button coupon_commit;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    try {
                        JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                        String orderid = jsonObject.getString("orderid");
                        Intent i = new Intent();
                        i.putExtra("orderid", orderid);
                        setResult(Constant.PAY_SUCCESS, i);
                        Coupon.this.finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        KeyBoard.showKeyBoard();
    }

    private void initView() {
        common_back.setOnClickListener(this);
        coupon_commit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                Coupon.this.finish();
                break;
            case R.id.coupon_commit:
                useCard();
                break;
            default:
                break;
        }
    }

    private void useCard() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("card_num", coupon_count_edt.getText().toString().trim());
        hashMap.put("password", coupon_pwd_edt.getText().toString().trim());
        new InterNetController(this, Constant.USECARD, handler, hashMap, 0);
    }
}
